{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  1.直接导包（老师代码直接给出的）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集X:  [[1], [2], [2], [1], [5], [4], [3], [4], [7]]\n",
      "数据集Y:  [[1], [3], [1], [0], [2], [0], [1], [3], [3]]\n",
      "[[1.79929577]\n",
      " [0.57711268]\n",
      " [3.24084507]]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAlNUlEQVR4nO3deUBU5f4G8GdmWAcYQBZZFMQN3NgFd3PJrDBzKc0sNS1Ly6XlXn+WdtO0bt3StKy0zCzTNDW3MjVN0QxQUNAEBEUBF1CWYV9mzu8Pul4nmAFkOWdmns9fl/ccZp77BZ/g8M4cmaY0UwARERk9udgBiIioebDQiYhMBAudiMhEsNCJiEwEC52IyERYiPXEhWp1o86XyWQQBG7IqQtnYxjnYxjno58UZyMDoFKp6jxmND+hqxwcxI4gWZyNYZyPYZyPflKcjVyuv7aNptCJiMgwFjoRkYlgoRMRmQgWOhGRiWChExGZCBY6EZGJYKETEZkIFjoRUSs5fTMRB69Gt9jji/ZKUSIic5GafwnL4lZhz6WD8LLzQNwT+2BrYdPsz8NCJyJqIdnFN/DeqTX4LuVHaAQNbC1s8HjXKGgETYs8HwudiKiZ5ZcXYGXCF1h37juUayqgkCkwtfvjeC3sBXjaubfY87LQiYiaSWlVGT5P+hYfnfkS6soiAMCjnUZiYe+X0NmpQ4s/PwudiKiJqjRV2JSyE++dWoMbpbkAgPva9cOiiLkIce/ZajlY6ERE90graLEr/QCWx61CeuEVAECIW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+UgJikd3u7OeHvdLgDA6CFh6NW5ncjJyBhMGBGJ9T8eg0arhauTPZ6OGiB2JMnw83ZDaIAvln25Gwq5HO3btsEAI3gbAL70n4jIRPCPokREJoKFTkRkIljoREQmgoVORGQiWOhERCaChU5EZCJY6EREJuL/AejHWhSjgldyAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "系数 [[0.31338028]]\n",
      "截距 [0.54577465]\n",
      "评分函数 0.2535531370038413\n"
     ]
    }
   ],
   "source": [
    "from sklearn import linear_model     #导入线性模型\n",
    "import matplotlib.pyplot as plt       \n",
    "import numpy as np\n",
    "\n",
    "\n",
    "X = [[1], [2], [2], [1], [5], [4], [3], [4], [7]]\n",
    "Y = [[1], [3], [1], [0], [2], [0], [1], [3], [3]]\n",
    "#X=[[0.067732],[0.427810],[0.995731],[0.738336],[0.981083],[0.526171],[0.378887],[0.033859],[0.132791],[0.138306],[0.247809],[0.648270],[0.731209],[0.236833],[0.969788],[0.607492],[0.358622],[0.147846],[0.637820],[0.230372],[0.070237],[0.067154],[0.925577],[0.717733],[0.015371],[0.335070],[0.040486],[0.212575],[0.617218],[0.541196]]\n",
    "#Y=[[3.176513],[3.816464],[4.550095],[4.256571],[4.560815],[3.929515],[3.526170],[3.156393],[3.110301],[3.149813],[3.476346],[4.119688],[4.282233],[3.486582],[4.655492],[3.965162],[3.514900],[3.125947],[4.094115],[3.476039],[3.210610],[3.190612],[4.631504],[4.295890],[3.085028],[3.448080],[3.167440],[3.364266],[3.993482],[3.891471]]\n",
    "\n",
    "print('数据集X: ', X)\n",
    "print('数据集Y: ', Y)\n",
    "\n",
    "#回归训练\n",
    "clf = linear_model.LinearRegression() \n",
    "clf.fit(X, Y)\n",
    "\n",
    "#预测结果\n",
    "X2 = [[4], [0.1], [8.6]]\n",
    "Y2 = clf.predict(X2)\n",
    "print(Y2)\n",
    "   \n",
    "\n",
    "#绘制线性回归图形\n",
    "plt.plot(X, Y, 'ks')                 #绘制训练数据集散点图\n",
    "plt.plot(X2, Y2, 'g-')               #绘制预测数据集直线\n",
    "plt.show()\n",
    "\n",
    "\n",
    "print('系数', clf.coef_)\n",
    "print('截距', clf.intercept_)\n",
    "print('评分函数', clf.score(X, Y))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 手搓简单线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAQp0lEQVR4nO3dcYxlZX3G8e/j7qhTtU7iThQGdE0km6ioqxOsoTFEQhcsgQ3FCE2tGJttjLaSmm1c/4CUpKFmEysRI9kAFSyFWlg32wa7JZEESQp1dhddBTehBMMOVEZwQeqosP31j7lrlmF2752ZO3u5734/yc2e+553zvmd3JNnz7znPXNTVUiSht8rBl2AJKk/DHRJaoSBLkmNMNAlqREGuiQ1YvWgdrxmzZpau3btoHYvSUNp9+7dP6uq8YXWDSzQ165dy9TU1KB2L0lDKclPjrbOIRdJaoSBLkmNMNAlqREGuiQ1wkCXpEZ0neWS5NXAPcCrOv1vr6or5/V5FXAz8D7gKeCjVfVo36ttyI6902zdtZ/HD85y8tgomzesY+P6iUGXdcLzc9FKWunzq5dpi78GPlRVzyUZAe5N8u2quu+IPp8Efl5Vb0tyCfBF4KN9q7IxO/ZOs2X7PmafPwTA9MFZtmzfB2B4DJCfi1bS8Ti/ug651JznOm9HOq/5f3P3QuCmzvLtwNlJ0pcKG7R11/7ffqiHzT5/iK279g+oIoGfi1bW8Ti/ehpDT7IqyQPAk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9VySEeBe4LNVdd+AS1uyJH8FTAK/W1XnD7qepUryKDBZVUP/ME6Sm4DvVtX1SV4J/E5VHRxwWUuWZBVz3y/x/qpa6gOWLzFUV+hVdQ9zf2996FXVE1W1p7P8C+AhXvpdrS97Nee5ztuRzmt4rhLmSXIK8IfA9YOuRXOSvB74IHADQFX9ZpjDvONs4L/7GeYwZIHeqiRrmftykPsHXMqSdIYoHgCeBO6qqqE8jo4vA38N/N+A6+iHAv4jye4kmwZdzDK8FZgB/qEzFHZ9ktcMuqhlugS4td8bNdAHLMlrgTuAy6vq2UHXsxRVdaiq3sPc982ekWQoh8OSnA88WVW7B11Ln/x+Vb0XOA/4dGfIchitBt4LfK2q1gP/C3x+sCUtXWfI6ALgX/q9bQN9gDpjzncAt1TV9kHXs1ydX4PvBs4dcClLdSZwQWfs+TbgQ0n+cbAlLV1VTXf+fRL4FnDGYCtasgPAgSN+87uduYAfVucBe6rqp/3esIE+IJ2biTcAD1XVlwZdz1IlGU8y1lkeBc4BfjzQopaoqrZU1SlVtZa5X4m/U1V/MuCyliTJazo32+kMT/wBMJSzw6rqf4DHkqzrNJ0NDNXkgXkuZQWGW6DH7xR9uUhyK3AWsCbJAeDKqrphsFUt2ZnAx4B9nfFngC90vr91mJwE3NS5a/8K4JtVNdTT/RrxRuBbc9cNrAb+qar+fbAlLctfALd0hiseAT4x4HqWpPOf6znAn6/I9odp2qIk6egccpGkRhjoktQIA12SGmGgS1IjDHRJaoSBLkmNMNAlqRH/D9AjlvD34d5aAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始变量:\n",
      " 初始偏置b = 0.0\n",
      " 初始权重w = 0.0\n",
      " 初始的损失值 = 3.7777777777777777 \n",
      "\n",
      "epoch 0:loss=2.4201091906721537\n",
      "epoch 100:loss=1.0328370899508588\n",
      "epoch 200:loss=1.0210259078589086\n",
      "epoch 300:loss=1.0165018830912114\n",
      "epoch 400:loss=1.0147690505991371\n",
      "epoch 500:loss=1.0141053255755634\n",
      "epoch 600:loss=1.013851099671483\n",
      "epoch 700:loss=1.0137537237883838\n",
      "epoch 800:loss=1.0137164260053602\n",
      "epoch 900:loss=1.0137021398743982\n",
      "最终的参数值:\n",
      " 偏置b = 0.542090306605832\n",
      " 权重w = 0.3142500366848816\n",
      " 最终的损失值 = 1.0136967006299147 \n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAdaUlEQVR4nO3de3RU9fnv8fdDCBAUiRdEroJVWbb1Qkm911otUu9osaBVES0QqkddKqdita5jPaKLejtVZxII/CKIULlVKohotYIKGi6KgCgiPyEgBGgokUQgPOePjPxyg9xmsjM7n9daWcx8987ez14zfPiyZ+95zN0REZHk1yLoAkREJD4U6CIiIaFAFxEJCQW6iEhIKNBFREKiZVA7PuaYY7xHjx5B7V5EJCktWbJkm7t3qG5ZYIHeo0cP8vLygtq9iEhSMrP/PtgynXIREQkJBbqISEgo0EVEQkKBLiISEgp0EZGQqPEqFzNrA7wLtI6tP83dH660TmvgRaAPsB0Y6O7r415tiMxals+YeWvYVFhM5/Q0RvbrRf/eXYIuq9nT6yKJlOj3V20uW/wOuMjdi8wsFVhoZnPdfVG5dW4D/u3uJ5rZIOAJYGDcqgyZWcvyGTVjBcV7SwHILyxm1IwVAAqPAOl1kURqjPdXjadcvExR7Glq7Kfyd+5eDeTGHk8DLjYzi0uFITRm3poDL+r3iveWMmbemoAqEtDrIonVGO+vWp1DN7MUM1sObAXmu/viSqt0ATYAuPs+YCdwdDXbGWZmeWaWV1BQ0KDCk9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pBkgR5WZtaDsuYgiwMupV5ipyiWA1uB+e6elMcR8wzwv4H9AdcRDw68YWZLzGxY0MU0QE+gAJgQOxU2zswOC7qoBhoEvBzvjSrQA2ZmhwPTgbvd/T9B11Mf7l7q7mdQ1m/2TDNLytNhZnYFsNXdlwRdS5yc7+4/AS4Fbo+dskxGLYGfABF37w18C9wfbEn1FztldBXwSry3rUAPUOyc83TgJXefEXQ9DRX7b/DbwK8CLqW+zgOuip17ngJcZGaTgi2p/tw9P/bnVmAmcGawFdXbRmBjuf/5TaMs4JPVpcBSd98S7w0r0AMS+zAxB1jt7k8FXU99mVkHM0uPPU4D+gKfBVpUPbn7KHfv6u49KPsv8T/d/caAy6oXMzss9mE7sdMTlwBJeXWYu38DbDCzXrGhi4GkunigkutJwOkWqGVP0abCzF4GLgSOMbONwMPunhNsVfV2HnATsCJ2/hnggVj/1mTSCciNfWrfAvibuyf15X4h0RGYWTZvoCUw2d1fD7akBvlfwEux0xXrgCEB11MvsX9c+wLDE7L9ZLpsUUREDk6nXEREQkKBLiISEgp0EZGQUKCLiISEAl1EJCQU6CIiIaFAFxEJif8PO0wMVolCMDsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    " # 计算loss\n",
    "def liner_loss(w,b,data):\n",
    "    x = data[:,0]  # 代表的是第一列数据\n",
    "    y = data[:,1]  # 代表的是第二列数据\n",
    "    # 损失函数:使用的是均方误差(MES)损失\n",
    "    loss = np.sum((y - w * x - b) ** 2) / data.shape[0]\n",
    "    # 返回loss\n",
    "    return loss\n",
    " \n",
    "# 计算梯度并更新参数\n",
    "def liner_gradient(w,b,data,lr):\n",
    "    # 数据集行数\n",
    "    N = float(len(data))\n",
    "    # 提取数据\n",
    "    x = data[:,0]\n",
    "    y = data[:,1]\n",
    "    # 求梯度\n",
    "    dw = np.sum(-(2 / N) * x * (y - w * x -b))\n",
    "    db = np.sum(-(2 / N) * (y - w * x -b))\n",
    "    # 更新参数\n",
    "    w = w - (lr * dw)\n",
    "    b = b - (lr * db)\n",
    " \n",
    "    return w,b\n",
    " \n",
    " \n",
    "# 每次迭代做梯度下降\n",
    "def optimer(data,w,b,lr,epcoh):\n",
    "\n",
    "    for i in range(epcoh):\n",
    "        # 通过每次循环不断更新w,b的值\n",
    "        w,b = liner_gradient(w,b,data,lr)\n",
    "        # 每训练100次更新下loss值\n",
    "        if i % 100 == 0 :\n",
    "            print('epoch {0}:loss={1}'.format(i,liner_loss(w,b,data)))\n",
    " \n",
    "    return w,b\n",
    " \n",
    " \n",
    "# 绘图\n",
    "def plot_data(data,w,b):\n",
    "    x = data[:,0]\n",
    "    y = data[:,1]\n",
    "    y_predict = w * x + b\n",
    "    plt.plot(x, y, 'o')\n",
    "    plt.plot(x, y_predict, 'k-')\n",
    "    plt.show()\n",
    " \n",
    " \n",
    "def liner_regression():\n",
    "    \"\"\"\n",
    "    构建模型\n",
    "    \"\"\"\n",
    "    # 加载数据\n",
    "    data = np.loadtxt('C:/Users/yc/Desktop/data2.csv',delimiter=',',encoding='utf-8')\n",
    "    # 显示原始数据的分布\n",
    "    x = data[:, 0]\n",
    "    y = data[:, 1]\n",
    "    plt.plot(x, y, 'o')\n",
    "    plt.show()\n",
    " \n",
    "    # 初始化参数\n",
    "    lr = 0.01  # 学习率\n",
    "    epoch = 1000  # 训练次数\n",
    "    w = 0.0  # 权重\n",
    "    b = 0.0  # 偏置\n",
    "    # 输出各个参数初始值\n",
    "    print('初始变量:\\n 初始偏置b = {0}\\n 初始权重w = {1}\\n 初始的损失值 = {2} \\n'\\\n",
    "        .format(b,w,liner_loss(w,b,data)))\n",
    " \n",
    "    # 更新w和b\n",
    "    w,b = optimer(data,w,b,lr,epoch)\n",
    " \n",
    "    # 输出各个参数的最终值\n",
    "    print('最终的参数值:\\n 偏置b = {1}\\n 权重w = {2}\\n 最终的损失值 = {3} \\n'.format(epoch,b,w,liner_loss(w,b,data)))\n",
    "    # 显示\n",
    "    plot_data(data,w,b)\n",
    " \n",
    " \n",
    "liner_regression()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.使用最小二乘法的线性拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w is :  1.6314263024849451\n",
      "b is :  3.028630460428157\n",
      "loss is :  0.005966090414688466\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function matplotlib.pyplot.show(close=None, block=None)>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAob0lEQVR4nO3deWBU1d0+8OfOJJlkkkwyEGBCyMIaICyR4IIIERFBCwi4t9Rdq9RqtWn7q21ta6v2fY22fWuttgWFalsFBIMCagAhoCxKUiAgCWFJQjKAMlkmk5lk5t7fHyMxw72TmSSzz/P5S869Sb6H4JOTc885V3BY6iQQEVHYUwW7ACIi8g0GOhFRhGCgExFFCAY6EVGEYKATEUWImGB94eaWFq/vFQQBkhR9i3HY7+jCfkeXvvZbAKDT6RSvhcUIXZecHOwSgoL9ji7sd3Tpa79VKvexHRaBTkREnjHQiYgiBAOdiChCMNCJiCIEA52IKEIEbdkiEVGkKa4woqSxFg7BBrWkwcL0LBTlGwL29TlCJyLygeIKI9YbayCqbBAEQFTZsN5Yg+IKY8BqYKATEflASWMtIIiujYLobL/wxy/PQ7P8TQjnvvRLDZxyISK6SF+mThyCDYKbdgCIf/aPSPi/vwMAVCdrgT895+uyGehERN1dmDqBSoQAQBScUyeoQI+hrpY0EL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T50xI+TrMB6QkYdmts4Ncdf956nd3L/xzE26efWnYBzrgud+SJGFN6T5UHj8NlSDg+umTcGneiGCX7ROe+t5wrglvbPwEto5OCACWzJ6K8SPC+5nRP9ZtR9UpI8ztVugSE7BgZj4cDhEAMLNgLCRJwn8+2IPKmtOIi1XjrvlX9evfecgEOhER9U9ITLkQEVH/MdCJiCIEA52IKEIw0ImIIgQDnYgoQjDQiYgiBAOdiChC/H8hx9jizv+ffwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 导入数据\n",
    "data1 = np.genfromtxt('C:/Users/yc/Desktop/data1.csv', delimiter = ',') \n",
    "\n",
    "# 提取data1中的两列数据，分别作为x,y\n",
    "X = data1[:, 0]\n",
    "Y = data1[:, 1]\n",
    "\n",
    "# 用plt画出散点图\n",
    "plt.scatter(X,Y)\n",
    "plt.show\n",
    "# 定义损失函数\n",
    "# 损失函数是系数的函数。另外还要传入数据x，y,上面放进了data1中\n",
    "def loss(w, b, data1):\n",
    "    total_loss = 0\n",
    "    M = len(data1)\n",
    "    \n",
    "    #逐点计算平方损失误差， 然后求平均数\n",
    "    for i in range(M):\n",
    "        X = data1[i, 0]\n",
    "        Y = data1[i, 1]\n",
    "        total_loss += (Y - w*X -b) ** 2\n",
    "        \n",
    "    return total_loss/M\n",
    "\n",
    "# 定义算法拟合函数\n",
    "    \n",
    "# 先定义一个求均值的函数\n",
    "def average(data):\n",
    "    sum = 0\n",
    "    num = len(data)\n",
    "    for i in range(num):\n",
    "        sum += data[i]\n",
    "    return sum/num\n",
    "\n",
    "# 定义核心拟合函数\n",
    "def fit(data1):\n",
    "    M = len(data1)\n",
    "    x_bar = average(data1[:,0])    # x的平均值\n",
    "    \n",
    "    sum_yx = 0  # 解偏导=0，w式中的分子\n",
    "    sum_x2 = 0  # 同上 分母x2求和\n",
    "    sum_delta = 0   # b式中求和部分\n",
    "    for i in range(M):\n",
    "        X = data1[i, 0]\n",
    "        Y = data1[i, 1]\n",
    "        sum_yx += Y * (X - x_bar) # w式中分子\n",
    "        sum_x2 += X ** 2    # w式中分母x2部分\n",
    "    # 根据公式计算w（以上为求解w式中的值，准备工作）\n",
    "    w = sum_yx / (sum_x2 - M * (x_bar ** 2))    #w公式表达\n",
    "        \n",
    "    # 求解b的部分  \n",
    "    for i in range(M):\n",
    "        X = data1[i, 0]   \n",
    "        Y = data1[i, 1]\n",
    "        sum_delta += (Y - w*X)\n",
    "    # 根据公式求解b\n",
    "    b = sum_delta/M\n",
    "    \n",
    "    return w, b\n",
    "\n",
    "\n",
    "# 测试\n",
    "w, b = fit(data1)\n",
    "print('w is : ', w)\n",
    "print('b is : ', b)\n",
    "\n",
    "loss = loss(w, b, data1)\n",
    "print('loss is : ', loss)\n",
    "\n",
    "\n",
    "# 画出拟合曲线\n",
    "plt.scatter(X, Y)\n",
    "# 针对每一个x， 计算出预测的y值\n",
    "pre_y = w * X + b\n",
    "\n",
    "plt.plot(X, pre_y, c = 'r')\n",
    "plt.show\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
